package com.ruoyi.web.controller.mall.orderOrder.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.controller.mall.orderOrder.domain.OrderOrder;
import com.ruoyi.web.controller.mall.orderOrder.service.IOrderOrderService;
import com.ruoyi.web.controller.mall.orderShipping.domain.OrderShipping;
import com.ruoyi.web.controller.mall.orderShipping.service.IOrderShippingService;
import com.ruoyi.web.controller.shop.shop.domain.ShopShop;
import com.ruoyi.web.controller.shop.shop.service.IShopShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * 订单信息Controller
 * 
 * @author ruoyi
 * @date 2021-01-18
 */
@RestController
@RequestMapping("/mall/orderOrder")
public class OrderOrderController extends BaseController
{
    @Autowired
    private IOrderOrderService orderOrderService;
    @Autowired
    private IOrderShippingService orderShippingService;
    @Autowired
    private IShopShopService shopShopService;

    /**
     * 查询订单信息列表
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Map<String,Object> map)
    {
        if (map.containsKey("status")){
            if (Integer.parseInt(map.get("status").toString())==0){
                map.put("status","3,4,5,6,7");
            }
        }
        startPage();
        List<Map<String,Object>> list = orderOrderService.selectOrderOrderList(map);
        return getDataTable(list);
    }
    /**
     * 获取订单信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        Map<String,Object> map = orderOrderService.queryByOrderId(id);
        return AjaxResult.success(map);
    }

    /**
     * 订单发货 单个
     */
    @PostMapping(value = "/deliverGoods")
    public AjaxResult deliverGoods(@RequestBody Map<String,Object> map)
    {
        String orderId = map.get("orderId").toString();
        String shippingName = map.get("shippingName").toString();
        String shippingCode = map.get("shippingCode").toString();
        String shopId = map.get("shopId").toString();
        OrderOrder oo = orderOrderService.selectOrderOrderById(orderId);
        if (oo==null){
            return AjaxResult.error("订单信息异常");
        }
        if (oo.getStatus()!=3){
            return AjaxResult.error("该订单不是待发货状态不能发货");
        }
        if (!shopId.equals(oo.getShopId())){
            return AjaxResult.error("该订单不属于你的店");
        }
        OrderOrder orderOrder = new OrderOrder();
        orderOrder.setId(orderId);
        orderOrder.setShippingName(shippingName);
        orderOrder.setShippingCode(shippingCode);
        orderOrder.setConsignAt(DateUtils.getNowDate());
        orderOrder.setStatus(4);
        orderOrder.setShopId(shopId);
        orderOrderService.updateOrderOrder(orderOrder);
        return AjaxResult.success("发货成功");
    }

    /**
     * 订单修改收货地址
     */
    @PostMapping(value = "/editAddress")
    public AjaxResult editAddress(@RequestBody Map<String,Object> map)
    {
        String orderId = map.get("orderId").toString();
        String receiverName = map.get("receiverName").toString();
        String receiverPhone = map.get("receiverPhone").toString();
        String receiverProvince = map.get("receiverProvince").toString();
        String receiverCity = map.get("receiverCity").toString();
        String receiverCounty = map.get("receiverCounty").toString();
        String receiverAddress = map.get("receiverAddress").toString();
        String shopId = map.get("shopId").toString();
        OrderOrder oo = orderOrderService.selectOrderOrderById(orderId);
        if (oo==null){
            return AjaxResult.error("订单信息异常");
        }
        if (oo.getStatus()!=3){
            return AjaxResult.error("只有待发货订单才能修改收货信息");
        }
        if (!shopId.equals(oo.getShopId())){
            return AjaxResult.error("该订单不属于你的店");
        }
        OrderShipping orderShipping = new OrderShipping();
        orderShipping.setReceiverName(receiverName);
        orderShipping.setReceiverPhone(receiverPhone);
        orderShipping.setReceiverProvince(receiverProvince);
        orderShipping.setReceiverCity(receiverCity);
        orderShipping.setReceiverCounty(receiverCounty);
        orderShipping.setReceiverAddress(receiverAddress);
        orderShipping.setOrderId(orderId);
        orderShipping.setUpdateAt(DateUtils.getNowDate());
        orderShippingService.updateOrderShippingByOrderId(orderShipping);
        return AjaxResult.success("修改成功");
    }

    /**
     * 订单确认开票
     */
    @PostMapping(value = "/enterBilling")
    public AjaxResult enterBilling(@RequestBody Map<String,Object> map)
    {
        String orderId = map.get("orderId").toString();
        String receiverName = map.get("receiverName").toString();
        String shopId = map.get("shopId").toString();
        OrderOrder oo = orderOrderService.selectOrderOrderById(orderId);
        if (oo==null){
            return AjaxResult.error("订单信息异常");
        }
        if (oo.getStatus()!=3){
            return AjaxResult.error("只有待发货订单才能修改收货信息");
        }
        if (!shopId.equals(oo.getShopId())){
            return AjaxResult.error("该订单不属于你的店");
        }
        OrderOrder orderOrder = new OrderOrder();
        orderOrder.setId(shopId);
        orderOrder.setUpdateAt(DateUtils.getNowDate());
        orderOrderService.updateOrderOrder(orderOrder);
        return AjaxResult.success("修改成功");
    }


    /**
     * 导出订单信息列表
     */
//    @RequiresPermissions("mall:orderOrder:export")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(OrderOrder orderOrder)
    {
//        List<OrderOrder> list = orderOrderService.selectOrderOrderList(orderOrder);
//        ExcelUtil<OrderOrder> util = new ExcelUtil<OrderOrder>(OrderOrder.class);
//        return util.exportExcel(list, "orderOrder");
        return null;
    }

    /**
     * 修改保存订单信息
     */
//    @RequiresPermissions("mall:orderOrder:edit")
//    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
//    @PostMapping("/edit")
//    public AjaxResult editSave(OrderOrder orderOrder)
//    {
//        return toAjax(orderOrderService.updateOrderOrder(orderOrder));
//    }



    /**
     * 销量统计
     */
//    @RequiresPermissions("mall:orderOrder:edit")
    @PostMapping("/count/info")
    public AjaxResult countInfo(@RequestBody Map<String,Object> map)
    {
//        String shopId = map.get("shopId").toString();
        String beginAt = map.get("beginAt").toString();
        String endAt = map.get("endAt").toString();
        Date beginTime = null;
        Date endTime = null;
        Calendar c = Calendar.getInstance();
        if (StringUtils.isNotEmpty(beginAt)){
            beginAt = beginAt+" 00:00:00";
            endAt = endAt+" 23:59:59";

            beginTime = DateUtils.parseDate(beginAt);
            endTime = DateUtils.parseDate(endAt);
        }else {
            c.setTime(DateUtils.getNowDate());
            c.add(Calendar.DATE, -7);
            String day = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,c.getTime());
            beginAt = day+" 00:00:00";
            endAt = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,DateUtils.getNowDate())+" 23:59:59";

            beginTime = DateUtils.parseDate(beginAt);
            endTime = DateUtils.parseDate(endAt);
        }
        List<Integer> numList = new ArrayList<>();
        List<BigDecimal> moneyList = new ArrayList<>();
        List<String> dayList = new ArrayList<>();
        map.put("status","3,4,5,6,7");
        while (beginTime.getTime()<=endTime.getTime()){
//            c.add(Calendar.DATE, 1);
            String day = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,beginTime);
            beginAt = day +" 00:00:00";
            endAt = day + " 23:59:59";
            dayList.add(day);
            map.put("beginAt",beginAt);
            map.put("endAt",endAt);
            Map<String,Object> cm = orderOrderService.queryByCountDay(map);
            numList.add(Integer.parseInt(cm.get("num").toString()));
            moneyList.add(new BigDecimal(cm.get("money").toString()));
            c.setTime(beginTime);
            c.add(Calendar.DATE, 1);
            beginTime = c.getTime();
        }
        Map<String,Object> rm = new HashMap<>();
        rm.put("dayList",dayList);
        rm.put("numList",numList);
        rm.put("moneyList",moneyList);
        return AjaxResult.success(rm);
    }

    /**
     * 销量统计列表
     */
//    @RequiresPermissions("mall:orderOrder:edit")
    @PostMapping("/count/list")
    public TableDataInfo countList(@RequestBody Map<String,Object> map)
    {
        startPage();
//        String shopId = map.get("shopId").toString();
        String beginAt = map.get("beginAt").toString();
        String endAt = map.get("endAt").toString();
        Calendar c = Calendar.getInstance();

        if (StringUtils.isNotEmpty(beginAt)){
            beginAt = beginAt+" 00:00:00";
            endAt = endAt+" 23:59:59";
            map.put("beginAt",beginAt);
            map.put("endAt",endAt);
        }else {
            c.setTime(DateUtils.getNowDate());
            c.add(Calendar.DATE, -7);
            String day = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,c.getTime());
            beginAt = day+" 00:00:00";
            endAt = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,DateUtils.getNowDate())+" 23:59:59";
            map.put("beginAt",beginAt);
            map.put("endAt",endAt);
        }
        map.put("status","3,4,5,6,7");
        List<Map<String,Object>> list = orderOrderService.selectOrderOrderList(map);
        return getDataTable(list);
    }


    /**
     * 今日销售
     */
//    @RequiresPermissions("mall:orderOrder:edit")
    @PostMapping("/index/list")
    public AjaxResult indexList(@RequestBody Map<String,Object> map)
    {
        String shopId = map.get("shopId").toString();
        String day = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,DateUtils.getNowDate());
        String beginAt = day+" 00:00:00";
        String endAt = day+" 23:59:59";
        map.put("beginAt",beginAt);
        map.put("endAt",endAt);
        List<Map<String,Object>> list = orderOrderService.countByToday(map);
        List<String> nameList = new ArrayList<>();
        List<Integer> numList = new ArrayList<>();
        List<BigDecimal> moneyList = new ArrayList<>();
        if (list.size()>0){
            for (Map<String,Object> m : list){
                nameList.add(m.get("name").toString());
                numList.add(Integer.parseInt(m.get("num").toString()));
                moneyList.add(new BigDecimal(m.get("money").toString()));
            }
        }else {
            nameList.add("无");
            numList.add(0);
            moneyList.add(new BigDecimal("0"));
        }
        //查询今日营业额
        BigDecimal todayMoney = orderOrderService.todayMoney(map);
        map.remove("beginAt");
        map.remove("endAt");

        //历史订单数
        Integer allNum = orderOrderService.allNum(map);
        //历史总营业额& 可提现余额
//        BigDecimal allMoney = orderOrderService.todayMoney(map);
        ShopShop ss = shopShopService.selectShopShopById(shopId);

        map.clear();
        map.put("nameList",nameList);
        map.put("numList",numList);
        map.put("moneyList",moneyList);
        map.put("todayMoney",todayMoney);

        map.put("allNum",allNum);
        if (ss!=null){
            map.put("allMoney",ss.getAllMoney());
            map.put("myMoney",ss.getMoney());
        }else {
            map.put("allMoney","0");
            map.put("myMoney","0");
        }
        return AjaxResult.success(map);
    }

    /**
     * 开票确认
     */
//    @RequiresPermissions("mall:orderOrder:edit")
    @PostMapping("/invoice")
    public AjaxResult invoice(@RequestBody Map<String,Object> map)
    {
        String orderId = map.get("orderId").toString();
        Integer check = Integer.parseInt(map.get("check").toString());
        OrderOrder oo = orderOrderService.selectOrderOrderById(orderId);
        if (oo.getSee()!=1){
             return AjaxResult.error("只有申请状态才能确认开票");
        }
        if (check==2){
            OrderOrder orderOrder = new OrderOrder();
            orderOrder.setId(orderId);
            orderOrder.setSee(2);
            orderOrder.setCloseAt(DateUtils.getNowDate());
            orderOrderService.updateOrderOrder(orderOrder);
            return AjaxResult.success("确认成功");
        }
        if (check==3){
            OrderOrder orderOrder = new OrderOrder();
            orderOrder.setId(orderId);
            orderOrder.setSee(3);
            orderOrder.setCloseAt(DateUtils.getNowDate());
            orderOrderService.updateOrderOrder(orderOrder);
            return AjaxResult.success("拒绝成功");
        }
        return AjaxResult.success();
    }

    /**
     * 确认退货
     */
//    @RequiresPermissions("mall:orderOrder:edit")
    @PostMapping("/back")
    public AjaxResult orderBack(@RequestBody Map<String,Object> map)
    {
        String orderId = map.get("orderId").toString();
        Integer check = Integer.parseInt(map.get("check").toString());
        OrderOrder oo = orderOrderService.selectOrderOrderById(orderId);
        if (oo.getStatus()!=5){
            return AjaxResult.error("只有用户申请退货才能确认退货");
        }
        //1-同意，2-拒绝
        if (check==1){
            OrderOrder orderOrder = new OrderOrder();
            orderOrder.setId(orderId);
            orderOrder.setStatus(6);
            orderOrder.setReturnAt(DateUtils.getNowDate());
            orderOrderService.updateOrderOrder(orderOrder);
            return AjaxResult.success("确认成功");
        }
        if (check==2){
            OrderOrder orderOrder = new OrderOrder();
            orderOrder.setId(orderId);
            orderOrder.setStatus(4);
            orderOrder.setReturnAt(DateUtils.getNowDate());
            orderOrderService.updateOrderOrder(orderOrder);
            return AjaxResult.success("拒绝成功");
        }
        return AjaxResult.success();
    }

}
